/**
 * 
 */
package com.wolfpire.info.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wolfpire.info.model.ProfessionalCountInfo;
import com.wolfpire.info.model.ProfessionalDetailCountInfo;
import com.wolfpire.info.service.ProfessionalCountInfoService;
import com.wolfpire.system.common.AuthorityUtils;
import com.wolfpire.system.common.EasyuiPage;
import com.wolfpire.system.common.EasyuiPagination;
import com.wolfpire.system.common.Page;
import com.wolfpire.system.common.Result;
import com.wolfpire.system.common.springmvc.PrincipalArgument;
import com.wolfpire.system.common.util.DataUtils;
import com.wolfpire.system.common.util.StringUtils;
import com.wolfpire.system.model.Authority;
import com.wolfpire.system.model.User;

/**
 * @author lihd
 *
 */

@Controller
@RequestMapping("/professionalCountInfo")
public class ProfessionalCountInfoController {
	
	@Resource private ProfessionalCountInfoService professionalCountInfoService;
	
	@RequestMapping("/list")
	public ModelAndView list(@PrincipalArgument User user, @RequestParam Long menuId) {
		List<Authority> operAuthorities = AuthorityUtils.getAperAuthorities(user, menuId);
		return new ModelAndView("info/professionalCountInfo/professionalCountInfo_list", "operAuthorities", operAuthorities);
	}
	
	@ResponseBody
	@RequestMapping("/queryList")
	public EasyuiPagination<ProfessionalCountInfo> queryList(EasyuiPage<ProfessionalCountInfo>page, ProfessionalCountInfo filterProfessionalCountInfo) {
		Page<ProfessionalCountInfo> professionalCountInfos = this.professionalCountInfoService.setPageDataList(page, filterProfessionalCountInfo);
		return new EasyuiPagination<ProfessionalCountInfo>(professionalCountInfos);
	}
	
	@RequestMapping(value = "/add")
	public ModelAndView add(HttpSession session, ProfessionalCountInfo professionalCountInfo) {
		ModelAndView mv = new ModelAndView("info/professionalCountInfo/professionalCountInfo_add");
		mv.getModelMap().addAttribute(professionalCountInfo);
		return mv;
	}
	
	@ResponseBody
	@RequestMapping("/save")
	public Result save(
			@PrincipalArgument User user,
			ProfessionalCountInfo professionalCountInfo,
			String[] professionCatalog,
			String[] professionName,
			String[] professionCode,
			String[] isImportant,
			String[] existCommittee,
			String[] subtotal,
			String[] firstYearNum,
			String[] secondYearNum,
			String[] thirdYearNum,
			String[] apprenticeNum,
			String[] orderNum,
			String[] graduateNum,
			String[] doubleCertifyPercent,
			String[] employmentNum,
			String[] employmentPercent,
			String[] employmentMatchNum,
			String[] employmentMatchPercent,
			String[] firAvgSalary) {
		try {
			professionalCountInfo.setCreateUserId(user.getId());
			professionalCountInfo.setCreateUserName(user.getNickName());
			List<ProfessionalDetailCountInfo> professionalDetailCountInfos = new ArrayList<ProfessionalDetailCountInfo>();
			for (int i = 0; i < professionName.length; i++) {
				ProfessionalDetailCountInfo d = new ProfessionalDetailCountInfo();
				d.setProfessionCatalog(StringUtils.trimToEmpty(professionCatalog[i]));
				d.setProfessionName(StringUtils.trimToEmpty(professionName[i]));
				d.setProfessionCode(StringUtils.trimToEmpty(professionCode[i]));
				d.setIsImportant(StringUtils.trimToEmpty(isImportant[i]));
				d.setExistCommittee(StringUtils.trimToEmpty(existCommittee[i]));
				d.setSubtotal(DataUtils.convert(subtotal[i]));
				d.setFirstYearNum(DataUtils.convert(firstYearNum[i]));
				d.setSecondYearNum(DataUtils.convert(secondYearNum[i]));
				d.setThirdYearNum(DataUtils.convert(thirdYearNum[i]));
				d.setApprenticeNum(DataUtils.convert(apprenticeNum[i]));
				d.setOrderNum(DataUtils.convert(orderNum[i]));
				d.setGraduateNum(DataUtils.convert(graduateNum[i]));
				d.setDoubleCertifyPercent(StringUtils.trimToEmpty(doubleCertifyPercent[i]));
				d.setEmploymentNum(DataUtils.convert(employmentNum[i]));
				d.setEmploymentPercent(StringUtils.trimToEmpty(employmentPercent[i]));
				d.setEmploymentMatchNum(DataUtils.convert(employmentMatchNum[i]));
				d.setEmploymentMatchPercent(StringUtils.trimToEmpty(employmentMatchPercent[i]));
				d.setFirAvgSalary(StringUtils.trimToEmpty(firAvgSalary[i]));
				d.setSeq(i);
				professionalDetailCountInfos.add(d);
			}
			this.professionalCountInfoService.saveOrUpdate(professionalCountInfo, professionalDetailCountInfos );
			return new Result(true, "保存成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "保存失败");
		}
	}
	
	@RequestMapping("/edit")
	public ModelAndView edit(Long id) {
		ModelAndView mv = new ModelAndView("info/professionalCountInfo/professionalCountInfo_add");
		if (null != id) {
			ProfessionalCountInfo professionalCountInfo = this.professionalCountInfoService.get(id);
			mv.addObject(professionalCountInfo);
		}
		return mv;
	}
	
	@RequestMapping("/detail")
	public ModelAndView detail(@RequestParam(value = "id", required = true) Long id) {
		ModelAndView mv = new ModelAndView("info/professionalCountInfo/professionalCountInfo_detail");
		try {
			if (null != id) {
				ProfessionalCountInfo professionalCountInfo = this.professionalCountInfoService.get(id);
				mv.addObject(professionalCountInfo);
			}
			return mv;
		} catch (Exception e) {
			e.printStackTrace();
			return new ModelAndView("/proms/common/error");
		}
	}
	
	@ResponseBody
	@RequestMapping("/delete")
	public Result delete(@RequestParam(value="id") Long id) {
		try {
			this.professionalCountInfoService.del(id);
			return new Result(true, "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "删除失败");
		}
	}
	
	@ResponseBody
	@RequestMapping("/getDetails")
	public List<ProfessionalDetailCountInfo> getDetails(@RequestParam(value="id") Long id) {
		return this.professionalCountInfoService.getProfessionalDetailCountInfos(id);
	}
}
